<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\MConfig;
use App\Models\MLog;
use App\Models\MVip_award;
use App\Utils\ComUtil;
use App\VModels\VVip;
use Illuminate\Http\Request;

/**
 * 模式管理控制器
 */
class ModeController extends Controller
{
    /**
     * 模式设置界面
     */
    public function settingPage()
    {
        return view("admin.mode_setting");
    }

    /**
     * 模式配置信息保存
     */
    public function settingSave(Request $request)
    {
        if (MConfig::saveKV("config_mode_setting", $request->all())) {
            $this->data = $this->makeJsCode(0, "保存成功！");
            ComUtil::initConfig2Cache("config_mode_setting", true);   //需手工刷新一下配置信息
            Mlog::log("修改了模式配置",5);
        } else {
            $this->data = $this->makeJsCode(1, "保存失败！");
        }
        return response()->json($this->data);
    }

    /**
     * 对公收款账户设置界面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function receiptSettingPage()
    {
        return view("admin.receipt_setting");
    }

    /**
     * 对公账号 模式配置信息保存
     */
    public function receiptSettingSave(Request $request)
    {

        $array = [];
        //接收银行卡
        $card = null;
        if ($request->hasFile('card_image') && $request->file('card_image')->isValid()) {
            $file = $request->file('card_image');
            //验证图片格式
            $ext = strtolower($file->getClientOriginalExtension());
            if ($ext !== "jpg" && $ext !== "jpeg" && $ext !== "png") {
                $this->data = $this->makeJsCode(1, "$ext" . "文件不允许上传，请选择JPG、JPEG、PNG格式图片！");
                return response()->json($this->data);
            }

            $filename = md5_file($file->getPathname()) . "." . $ext;   //md5 + 扩展名
            $file->move('uploads/imgs/', $filename);
            $card_img = '/uploads/imgs/' . $filename;

        } else {
            $card_img = $request->old_yhk;
        }

        $array["card_image"] = $card_img;
        $array["card_no"] = $request->card_no;
        $array["card_name"] = $request->card_name;
        $array["bank_name"] = $request->bank_name;

//        var_dump($request->old_yhk);
//        var_dump($request->all());die;
        //接收微信二维码
        $erwei = null;
        if ($request->hasFile('wx_image') && $request->file('wx_image')->isValid()) {
            $file = $request->file('wx_image');
            //验证图片格式
            $ext = strtolower($file->getClientOriginalExtension());
            if ($ext !== "jpg" && $ext !== "jpeg" && $ext !== "png") {
                $this->data = $this->makeJsCode(1, "$ext" . "文件不允许上传，请选择JPG、JPEG、PNG格式图片！");
                return response()->json($this->data);
            }

            $filename = md5_file($file->getPathname()) . "." . $ext;   //md5 + 扩展名
            $file->move('uploads/imgs/', $filename);
            $erwei = '/uploads/imgs/' . $filename;

        } else {
            $erwei = $request->old_wx;
        }
        $array["wx_image"] = $erwei;
        $array["wx_no"] = $request->wx_no;


        $zfb = null;
        if ($request->hasFile('alipay_image') && $request->file('alipay_image')->isValid()) {
            $file = $request->file('alipay_image');
            //验证图片格式
            $ext = strtolower($file->getClientOriginalExtension());
            if ($ext !== "jpg" && $ext !== "jpeg" && $ext !== "png") {
                $this->data = $this->makeJsCode(1, "$ext" . "文件不允许上传，请选择JPG、JPEG、PNG格式图片！");
                return response()->json($this->data);
            }

            $filename = md5_file($file->getPathname()) . "." . $ext;   //md5 + 扩展名
            $file->move('uploads/imgs/', $filename);
            $zfb = '/uploads/imgs/' . $filename;
        } else {
            $zfb = $request->old_zfb;
        }
        $array["alipay_image"] = $zfb;
        $array["alipay_no"] = $request->alipay_no;
//        $array["alipay_name"] = $request->alipay_name;

//        var_dump($array);die;

        if (MConfig::saveAttr($request->_title, $array)) {

            $this->data = $this->makeJsCode(0, "保存成功！");
            MLog::log("编辑对公账户设置",5);
//            ComUtil::initConfig2Cache($request->config_title, true);   //需手工刷新一下配置信息
        } else {
            $this->data = $this->makeJsCode(1, "保存失败！");
        }
        return response()->json($this->data);
    }

    /**
     * 系统功能开关设置页
     */
    public function systemSwitch()
    {
        return view("admin.system_switch");
    }

    /**
     * 邮件配置页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function emailSettingPage()
    {
        return view("admin.email_setting");
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function vipConfigPage()
    {
        $this->data['vip_awards'] = MVip_award::all();
        return view("admin.vipConfig")->with($this->data);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function vipConfigEdit($id = 0)
    {

        $this->data['my_vip_award'] = MVip_award::find($id);
        $this->data['vip_awards'] = MVip_award::all();
        return view("admin.vipConfigEdit")->with($this->data);
    }

    /**
     * @param Request $request
     * @param int $id
     * @return $this
     */
    public function vipConfigDel(Request $request, $id = 0)
    {

        $my_vip_award = MVip_award::find($id);
        if (!empty($my_vip_award)) {
            $my_vip_award->delete();
            $this->data = $this->makeJsCode(0, "删除成功！");
        } else {
            $this->data = $this->makeJsCode(1, "删除失败！");
        }
        return back()->with($this->data);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function vipConfigSave(Request $request, $id)
    {

        if (!$request->has(['vip', 'name', 'award'])) {
            $this->data = $this->makeJsCode(1, "请填写完整信息");
            return response()->json($this->data);
        }
        $my_vip_award = MVip_award::find($id);

        if (empty($my_vip_award)) {
            $my_vip_award = new MVip_award();

            $result = $my_vip_award->where("name", $request->name)->first();

            if ($result) {
                $this->data = $this->makeJsCode(1, "会员等级名称已经存在!!!");
                return response()->json($this->data);
            }

            $request_vip = MVip_award::max("vip") + 1;
        } else {
            $request_vip = $my_vip_award->vip;
        }

        if ($my_vip_award->vip_award_save($request_vip, $request->name, $request->award)) {

            $this->data = $this->makeJsCode(0, "保存成功");
            return response()->json($this->data);
        }
    }

    /**
     * 会员等级设定页面
     *
     * @param string $slug 会员等级标识，不为 "" 代表编辑指定会员等级
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function vipsPage($slug = "")
    {
        $vips = VVip::getVipList();
        $this->data['vips'] = empty($vips) ? [] : $vips;
        $this->data['vip'] = VVip::vipBySlug($slug);
        return view("admin.vip_setting")->with($this->data);
    }

    /**
     * 会员等级参数存储
     */
    public function vipsSave(Request $request)
    {
        if (!$this->validateReq($request,
            [
                "slug" => "required",
                "name" => "required",
                "attr_vals" => "required",
            ],
            [
                "slug.required" => "等级标识字段必须填写！",
                "name.required" => "等级名称字段必须填写！",
                "attr_vals.required" => "请必须为等级添加至少一个属性！",
            ])
        ) {
            $this->data = $this->makeJsCode(1, $this->errors->first());
            return response()->json($this->data);
        }

        $vips = MConfig::getInstance(MConfig::TITLE_VIP);
        if (empty($vips)) {
            MConfig::saveAttr(MConfig::TITLE_VIP, []);
            $vips = MConfig::getInstance(MConfig::TITLE_VIP);
        }
        $vip = array();
        $vip['slug'] = $request->slug;
        $vip['name'] = $request->name;
        $attrs = array();

        $jsonArr = explode("-", $request->attr_vals);
        foreach ($jsonArr as $jsonStr) {
            $arr = json_decode($jsonStr, true);
            $attrs[$arr['key']] = $arr;
        }
        $vip['attrs'] = $attrs;

        $vips->kvList[$request->slug] = $vip;

        if ($vips->saveSelf()) {
            $this->data = $this->makeJsCode(0, "保存会员等级成功！");
        } else {
            $this->data = $this->makeJsCode(1, "保存会员等级失败！");
        }
        return response()->json($this->data);
    }

    /**
     * 删除指定的会员等级信息
     *
     * @param string $slug 会员等级标识
     *
     * @return mixed
     */
    public function vipsDel($slug)
    {
        if (empty($slug)) {
            $this->data = $this->makeJsCode(1, "指定的会员等级不存在！");
            return back()->with($this->data);
        }

        $vips = MConfig::getInstance(MConfig::TITLE_VIP);
        if (empty($vips)) {
            $this->data = $this->makeJsCode(1, "指定的会员等级不存在！");
            return back()->with($this->data);
        }
        //删除指定数组元素
        unset($vips->kvList[$slug]);
        if ($vips->saveSelf()) {
            $this->data = $this->makeJsCode(0, "删除会员等级成功！");
        } else {
            $this->data = $this->makeJsCode(1, "删除会员等级失败！");
        }
        return back()->with($this->data);
    }
}